package com.chinackts.enterprise.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chinackts.common.exception.ApplicationException;
import com.chinackts.common.okhttp.OkHttpRequest;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author Lianhong
 * @since 2021/11/2
 */
@Service
public class EnterpriseService {

    private static final Logger LOGGER = LoggerFactory.getLogger(EnterpriseService.class);

    //企业备案信息
    private static final String ENT_QUALIFICATION_URL = "https://swapp.singlewindow.cn/njcfserver/sw/qual/queryQualInfo";
    //企业电子口岸卡信息
    private static final String IC_CARD_URL = "https://app.singlewindow.cn/userserver/user/verifyLimite/getBindedVerify";

    @Resource(name = "proxyOkHttpRequest")
    private OkHttpRequest request;

    public JSONObject qualification(String id) {
        JSONObject params = new JSONObject();
        params.put("limit", "10");
        params.put("offset", "0");
        params.put("scCode", "123");
        params.put("orgCode", "123");
        String response = request.post(ENT_QUALIFICATION_URL, params.toString());
        if (StringUtils.isBlank(response))
            throw new ApplicationException("8999", "获取企业基本信息失败");
        try {
            return JSON.parseObject(response);
        }catch (Exception e) {
            LOGGER.error("获取企业基本信息异常: {}", e.getMessage());
            throw new ApplicationException("9999", "获取企业基本信息异常，请重试。");
        }
    }

    public JSONObject icCard(String id) {
        JSONObject params = new JSONObject();
        params.put("limit", "10");
        params.put("offset", "0");
        params.put("_", DateTime.now().getMillis());
        String response = request.get(IC_CARD_URL, params);
        if (StringUtils.isBlank(response))
            throw new ApplicationException("电子口岸IC卡信息为空，请重试。");
        try {
            return JSON.parseObject(response);
        }catch (Exception e) {
            LOGGER.error("电子口岸IC卡信息获取异常: {}", e.getMessage());
            throw new ApplicationException("9999", "电子口岸IC卡信息获取异常，请重试。");
        }
    }
}
